<?php

require_once 'common.php';
require_once 'ResultData.php';
require_once 'Student.php';
require_once 'config.php';


if (isGet()) {
    echo "不得以GET访问";
    exit;
}
$result = new ResultData();

$size = trim($_REQUEST['size']); // 一页的数量

if ($size == "") {
    $result->code = 404;
    $result->msg = "size未填写";
    exit(json_encode($result));
}
$size = intval($size);
$page = trim($_REQUEST['page']); // 第几页的数量

if ($page == "") {
    $result->code = 404;
    $result->msg = "page未填写";
    $json_string = json_encode($result);
    exit($json_string);
}
$page = intval($page);


$con = mysqli_connect($servername, $username, $password, $database, $port);
if (!$con) {
    $result->code = 404;
    $result->msg = "数据库连接不上";
    exit(json_encode($result));
}

$sql = "SELECT * FROM t_student LIMIT " . ($page - 1) * $size . "," . $size;


$record = $con->query($sql);

$list = array();

if ($record->num_rows > 0) {
    while ($row = $record->fetch_assoc()) {
        $student = new Student();

        $student->id = $row['id'];
        $student->name = $row['name'];
        $student->sex = $row['sex'];
        $student->age = $row['age'];
        $student->edu = $row['edu'];
        $student->city = $row['city'];
        $student->no = $row['no'];
        array_push($list, $student);
    }
}


$result->data->list = $list;


$sqlCount = "SELECT COUNT(*) count FROM t_student";
$record = $con->query($sqlCount);
if ($record->num_rows > 0) {
    while ($row = $record->fetch_assoc()) {
        $result->data->count = $row['count'];
    }
}

mysqli_close($con);

exit(json_encode($result));




